<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
  String basePath = request.getContextPath();
  Object email = request.getSession().getAttribute("email");
  Object roleIdTemp = request.getSession().getAttribute("roleId");
  Integer roleId = -1;
  if (roleIdTemp != null) {
    roleId = Integer.parseInt(roleIdTemp.toString());
  }
%>

<html>
<head>
  <title>用户信息</title>
  <jsp:include page="../common/common-css.jsp"></jsp:include>
  <jsp:include page="../common/common-js.jsp"></jsp:include>
  <style type="text/css">
    .titleDesc {
      color: darkgrey;
    }

    td {
      padding: 15px
    }

    a {
      text-decoration: none
    }

    a:hover {
      text-decoration: none
    }

    a:link {
      text-decoration: none
    }

    a:visited {
      text-decoration: none
    }
  </style>
</head>
<body style="background-color: #f5f5f5">
<jsp:include page="../../head.jsp"></jsp:include>
<div class="container-fluid" style="padding: 0px">
  <div class="col-xs-0 col-sm-2 col-md-3 col-lg-4"></div>
  <div class="col-xs-12 col-sm-8 col-md-6 col-lg-4"
       style="background-color: #ffffff;margin-top: 30px;padding-left: 0px;padding-right: 0px;padding-bottom: 0px;">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" align="center" style="margin-top: 20px">
      <c:choose>
        <c:when test='${not empty userExtDto.logo}'>
          <img src="${userExtDto.logo}-120x120" alt="用户头像" class="img-responsive img-circle">
        </c:when>
        <c:otherwise>
          <img src="<%=basePath%>/img/user.png" alt="用户头像" class="img-responsive">
        </c:otherwise>
      </c:choose>
      <hr style="width: 100%;margin-bottom: 0px">
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <table style="width: 100%">
        <tr>
          <td>姓名</td>
          <td class="titleDesc" align="right">${userExtDto.name}</td>
        </tr>
        <tr style="border-top: 1px solid #ebebeb;">
          <td>邮箱</td>
          <td align="right" class="titleDesc">${userExtDto.email}</td>
        </tr>
        <tr style="border-top: 1px solid #ebebeb;">
          <td>收益</td>
          <td align="right" class="text-default-color">
            <small>￥</small>
            <span class="income"><i class="icon-spinner icon-spin text-default-color"></i>&nbsp;正在计算...</span>
          </td>
        </tr>
        <tr style="border-top: 1px solid #ebebeb;">
          <td style="width: 100px">地址</td>
          <td class="titleDesc" align="right">${userExtDto.addr}</td>
        </tr>
        <tr style="border-top: 1px solid #ebebeb;">
          <td>入驻时间</td>
          <td align="right" class="titleDesc">
            <fmt:formatDate value='${userExtDto.createtime}' pattern='yyyy-MM-dd HH:mm:ss'/></td>
        </tr>
        <% if (roleId != null && roleId == 2) {%>
        <tr style="border-top: 1px solid #ebebeb;">
          <td>手机号</td>
          <td class="titleDesc" align="right">${userExtDto.phone}</td>
        </tr>
        <% } %>
        <tr style="border-top: 1px solid #ebebeb;">
          <td align="right" colspan="2">
            <a href="<%=basePath%>/myStory?email=${userExtDto.email}">原创&nbsp;&nbsp;<i class="icon-angle-right"></i></a>
          </td>
        </tr>
        <tr style="border-top: 1px solid #ebebeb;">
          <td colspan="2">
            <textarea style="resize: none;width: 100%;border: none;color: darkgray"
                      rows="5" readonly>${userExtDto.remark}</textarea>
          </td>
        </tr>
        <tr style="border-top: 1px solid #ebebeb;">
          <td colspan="2">
            大好人，值得打赏!
            <small style="color: darkgray;padding-left: 10px">微信或支付宝扫码打赏.</small>
          </td>
        </tr>
        <tr>
          <td colspan="2" style="padding: 0px">
            <table style="width: 100%">
              <tr style="border-top: 1px solid #ebebeb;">
                <td align="center" style="width: 50%;padding: 20px">
                  <p>
                    <c:choose>
                      <c:when test="${userExtDto.weixinQrcode == null}">
                        <img src="<%=basePath%>/img/qrcode.jpg" class="img-responsive img-rounded weixinQrcode"
                             style="width: 120px;height: 120px;border: none">
                      </c:when>
                      <c:otherwise>
                        <img src="${userExtDto.weixinQrcode}-120x120" class="img-responsive img-rounded weixinQrcode"
                             style="width: 120px;height: 120px;border: none">
                      </c:otherwise>
                    </c:choose>
                  </p>

                  <p>
                    <small style="color: darkgray">微信打赏</small>
                  </p>
                </td>
                <td align="center" style="width: 50%;padding: 20px">
                  <p>
                    <c:choose>
                      <c:when test="${userExtDto.alipayQrcode == null}">
                        <img src="<%=basePath%>/img/qrcode.jpg" class="img-responsive img-rounded alipayQrcode"
                             style="width: 120px;height: 120px;border: none">
                      </c:when>
                      <c:otherwise>
                        <img src="${userExtDto.alipayQrcode}-120x120" class="img-responsive img-rounded alipayQrcode"
                             style="width: 120px;height: 120px;border: none">
                      </c:otherwise>
                    </c:choose>
                  </p>

                  <p>
                    <small style="color: darkgray">支付宝打赏</small>
                  </p>
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <%if (roleId == 1) {%>
        <c:if test="${userExtDto.role == 3 or userExtDto.role == 4 or userExtDto.role == 1}">
          <tr style="border-top: 1px solid #ebebeb;">
            <td>进入商户</td>
            <td class="titleDesc" align="right">
              <a href="<%=basePath%>/goods?email=${userExtDto.email}">
                <div class="text-right"><i class="icon-angle-right"></i></div>
              </a>
            </td>
          </tr>
        </c:if>
        <%}%>
      </table>
    </div>
  </div>
</div>
<%if (roleId == 1) {%>
<div class="col-xs-0 col-sm-2 col-md-3 col-lg-4"></div>
<div class="col-xs-12 col-sm-8 col-md-6 col-lg-4" style="background-color: #ffffff;margin-top: 10px;padding: 20px">
  <h4>用户角色</h4>
  <select class="shortselect" style="margin-top: 10px">
    <option value="1">超级管理员</option>
    <option value="2">商户</option>
    <option value="3">个人用户</option>
    <option value="4">大V</option>
  </select>
</div>
<%}%>
<%if (roleId == 2) {%>
<div class="col-xs-0 col-sm-2 col-md-3 col-lg-4"></div>
<div class="col-xs-12 col-sm-8 col-md-6 col-lg-4" style="background-color: #ffffff;margin-top: 10px;">
  <table style="width: 100%;background-color: #ffffff;">
    <tr style="border-bottom: 1px solid #eaeaea">
      <td style="color: darkgray;padding: 8px 15px">${userExtDto.email}</td>
      <td align="right" style="width: 110px;padding: 8px 15px">
        <a class="btn btn-default-color" href="javascript:sendCashCode();"> 发送提现码 </a>
      </td>
    </tr>
    <tr style="border-bottom: 1px solid #eaeaea">
      <td style="padding: 0px 15px">
        <input type="text" maxlength="6" onkeyup="this.value=this.value.replace(/\D/g,'')"
               onafterpaste="this.value=this.value.replace(/\D/g,'')" id="cashCode"
               placeholder="输入6位数字提现码"
               style="width: 100%;margin: 0px;padding: 0px;margin: 0px;border: none">
      </td>
      <td align="right" style="padding: 8px 15px">
        <a class="btn btn-default-color" href="javascript:cashAmount();">提&nbsp;&nbsp;&nbsp;&nbsp;现</a>
      </td>
    </tr>
  </table>
  <div style="color: darkgray;margin: 15px 15px">
    <p>提现说明</p>
    <ol>
      <li>发送提现码到用户邮箱</li>
      <li>根据用户收到的提现码提现</li>
      <li>提现金额不足100元不可提现</li>
      <li>提现码10分钟内有效</li>
    </ol>
  </div>
</div>
<%}%>
<jsp:include page="../common/common-html.jsp"></jsp:include>
<jsp:include page="../../foot.jsp"></jsp:include>
<div class="modal fade bs-example-modal-sm" id="registerSuccessModal" role="dialog" style="margin-top: 100px">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header" align="center">
        <b>提示</b>
      </div>
      <div class="modal-body text-center" style="padding: 30px;">
        <p>提现码已发送至用户邮箱!</p>
      </div>
      <div class="modal-footer" align="right">
        <button class="btn btn-default-color" id="IKnow">知道了</button>
      </div>
    </div>
  </div>
</div>
</body>


<script type="application/javascript">
  // - 发送提现码
  function sendCashCode() {
    var email = '<%=email%>';
    if (email == 'null') {
      $("#loginModal").modal();
      return;
    }
    var url = '<%=basePath%>/cashHistory/sendCashCode';
    var income = $(".income").text();
    if (income == 0) {
      alerErr("提现金额为零");
      return;
    }
    var param = {buyerEmail: '${userExtDto.email}', shopEmail: email, cashAmount: income};
    $.post(url, param, function (data, status) {
      if (data.code == 200) {
        $("#registerSuccessModal").modal({
          keyboard: false,
          backdrop: 'static'
        });
      } else {
        aler(data);
      }
    })
  }
  // - 提现
  function cashAmount() {
    var url = '<%=basePath%>/cashHistory/cashAmount';
    var cashCode = $.trim($("#cashCode").val());
    if (cashCode == '') {
      alerErr("输入提现码");
      return;
    }
    if (cashCode.length != 6) {
      alerErr("输入6位数字提现码");
      return;
    }
    var param = {
      buyerEmail: '${userExtDto.email}',
      shopEmail: '<%=email%>',
      cashAmount: $(".income").text(),
      cashCode: cashCode
    };
    $.post(url, param, function (data, status) {
      aler(data);
    })
  }
  // - 更新用户角色
  function updateUserRole(email, roleId) {
    var param = {email: email, roleId: roleId};
    var url = "<%=basePath%>/user/updateuserrole";
    $.post(url, param, function (data, status) {
      alerOK(data.msg);
    })
  }
  // - 加载用户角色
  function loadRole(roleId) {
    $(".shortselect").val(roleId);
  }

  // - 计算对应门店收益
  function calculateSingleIncome(buyerEmail, shopEmail, role) {
    if (role == 3 || role == 4) {
      // - 门店查看用户详情，显示门店收益
      var url = '<%=basePath%>/user/incomeSingleStatistic';
      var param = {buyerEmail: buyerEmail, shopEmail: shopEmail};
      $.post(url, param, function (data, status) {
        if (data.code == 200) {
          $(".income").text(data.data);
        } else {
          $(".income").text('计算失败...');
        }
      })
    } else {
      // - 显示总收益
      $(".income").text('${userExtDto.income}');
    }
  }

  $(function () {
    initStyle();
    // - 计算用户收益
    calculateSingleIncome('${userExtDto.email}', '<%=email%>', '<%=roleId%>');
    // - 动态加载用户角色
    loadRole(${userExtDto.role});
    // - 更新用户角色
    $(".shortselect").change(function () {
      updateUserRole('${userExtDto.email}', $(".shortselect").val());
    })

    $("#IKnow").click(function () {
      $("#registerSuccessModal").modal("hide");
    })
  })
</script>

</html>
